package com.qinbaokeji.checkpoint.dahuacamera;


import com.qinbaokeji.checkpoint.dahuacamera.config.MyUncaughtExceptionHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.channels.FileLock;

//@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication
public class DahuaApplication {
    private static FileLock lock = null;

    //判断该应用是否已启动
    public static boolean isRunning(String path){
        try{
            //获得实例标志文件
            File flagFile = new File(path);
            //如果不存在就新建一个
            if(!flagFile.exists()) flagFile.createNewFile();
            //获得文件锁
            lock = new FileOutputStream(flagFile).getChannel().tryLock();
            //返回空表示文件已被运行的实例锁定
            if(lock==null) return true;
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        return false;
    }


    public static void main(String[] args) throws InterruptedException {
        String path = args[1].replaceAll("\\\\","/")+"lock/"+args[0];
        if(isRunning(path)){
            System.out.println("已有一个程序在运行");
            Thread.sleep(5000);
            System.exit(-1);
        }

        SpringApplication.run(DahuaApplication.class,args);
        //当前线程异常默认处理程序
        Thread.currentThread().setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

    }
}

